package com.property.manage.controller.system;

import com.property.manage.model.system.MenuVo;
import com.property.manage.model.system.Role;
import com.property.manage.service.system.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 描述
 *
 * @author lwb
 * @since 2020/7/15
 */
@Api(tags = "角色管理")
@RestController
@Validated
@RequestMapping("/role")
@AllArgsConstructor
public class RoleController {

    private RoleService roleService;

    @ApiOperation("列表")
    @GetMapping("/list")
    public List<Role> list(@ApiParam("角色名称") @RequestParam(value = "roleName", required = false) String roleName){
        return roleService.getRoles(roleName);
    }

    @ApiOperation("菜单")
    @GetMapping("/menu")
    public List<MenuVo> menu(@ApiParam("角色编号") @RequestParam("roleId") @NotNull(message = "角色编号为空") Integer roleId){
        return roleService.getRoleMenu(roleId);
    }

    @ApiOperation("添加")
    @PostMapping("/add")
    public void add(@ApiParam("角色名称") @RequestParam(value = "roleName") @NotBlank(message = "角色名称为空") String roleName){
         roleService.add(roleName);
    }

    @ApiOperation("删除")
    @PostMapping("/delete")
    public void delete(@ApiParam("角色编号") @RequestParam("roleId") @NotNull(message = "角色编号为空") Integer roleId){
        roleService.delete(roleId);
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    public void update(@ApiParam("角色编号") @RequestParam("roleId") @NotNull(message = "角色编号为空") Integer roleId,
                       @ApiParam("角色名称") @RequestParam(value = "roleName") @NotBlank(message = "角色名称为空") String roleName){
        roleService.update(roleId, roleName);
    }

    @ApiOperation("修改菜单")
    @PostMapping("/update/menu")
    public void updateMenu(@ApiParam("角色编号") @RequestParam("roleId") @NotNull(message = "角色编号为空") Integer roleId,
                           @ApiParam("菜单编号列表") @RequestParam(value = "menuIds") @NotBlank(message = "菜单编号列表为空") String menuIds){
        roleService.updateMenu(roleId, menuIds);
    }
}
